<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拖动</title>
</head>
<body>
    <style type="text/css">
        img{
            position:absolute;
        }
        </style>
        <script src="./Vue.js"></script>
        </head>
        <body>
        <div id="box">
            <img src="./image/h.jpg">
        </div>
        <script type="text/javascript">
        var vm = new Vue({
            el:'#box',
            directives: {
                move: function (el) {
                    el.onmousedown = function(e) {
                        var initX = el.offsetLeft;
                        var initY = el.offsetTop;
                        var offsetX = e.clientX - initX;
                        var offsetY = e.clientY - initY;
                        document.onmousemove = function(e) {
                            var x = e.clientX - offsetX;
                            var y = e.clientY - offsetY;
                            var maxX = document.documentElement.clientWidth - el.offsetWidth;
                            var maxY = document.documentElement.clientHeight - el.offsetHeight;
                            if(x <= 0) x = 0;
                            if(y <= 0) y = 0;
                            if(x >= maxX) x = maxX;
                            if(y >= maxY) y = maxY;
                            el.style.left = x + "px";
                            el.style.top = y + "px";
                            return false;
                        }
                    }
                    document.onmouseup = function() {
                        document.onmousemove = null;
                    }
                }
            }
        })
        </script>
        
</body>
</html>